package com.panopset.sf.menu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenu;

import com.panopset.Dump;
import com.panopset.SysInfo;
import com.panopset.compat.Strings;
import com.panopset.nls.MsgProps;
import com.panopset.sf.Windoc;
import com.panopset.sf.TextFrame;

/**
 * Help menu.
 * 
 * @author Karl Dinwiddie
 * 
 */
public class HelpMenu extends AbstractMenu {

	/**
	 * 
	 * @param swingDoc
	 *            SwingDoc.
	 */
	public HelpMenu(final Windoc swingDoc) {
		super(swingDoc, Strings.capitalize(new MsgProps(
				Windoc.SD_MSG_PROPS).get("help")));
	}

	/**
	 * 
	 * @return Menu.
	 */
	protected void populateMenu(final JMenu menu) {

		menu.add(createMenuItem(Windoc.SD_MSG_PROPS, "about",
				new ActionListener() {
					public void actionPerformed(final ActionEvent e) {
						sd.sf.doAbout();
					}
				}));

		menu.add(createMenuItem(JAVA_PROPS, new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				doJavaProperties();
			}
		}));

		menu.add(createMenuItem(SYS_INFO, new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				doSystemInfo();
			}
		}));

		menu.add(createMenuItem(ENV_VARS, new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				doEnvVars();
			}
		}));

		menu.add(createMenuItem(SHOW_LOG, new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				doShowLog();
			}
		}));
	}

	private synchronized void doJavaProperties() {
		showTextDialog(JAVA_PROPS, new Dump(System.getProperties()).toString());
	}

	private synchronized void doSystemInfo() {
		showTextDialog(SYS_INFO, new Dump(SysInfo.getMap()).toString());
	}

	private synchronized void doEnvVars() {
		showTextDialog(ENV_VARS, new Dump(System.getenv()).toString());
	}

	private synchronized void doShowLog() {
		showTextDialog(SHOW_LOG, sd.sf.getLogHistory().toString());
	}

	private void showTextDialog(final String title, final String msg) {
		new TextFrame(sd.sf.getIcon16x16(), title).show(msg);
	}

	private static final String SHOW_LOG = "Show Log";
	private static final String JAVA_PROPS = "Java Properties";
	private static final String SYS_INFO = "System Information";
	private static final String ENV_VARS = "Environment Variables";

}
